package com.oil.petro.eshop.controller;

import com.oil.petro.eshop.bean.BasicBean;
import com.oil.petro.eshop.bean.ProductBean;
import com.oil.petro.eshop.bean.UserBean;
import com.oil.petro.eshop.entity.Account;
import com.oil.petro.eshop.entity.Person;
import com.oil.petro.eshop.service.CategoryService;
import com.oil.petro.eshop.service.ManufacturerService;
import com.oil.petro.eshop.service.ProductService;
import com.oil.petro.eshop.service.UserAccountService;
import com.oil.petro.eshop.utils.NavigationBar;
import com.oil.petro.eshop.utils.NavigationMenu;
import com.oil.petro.eshop.utils.PaginationUtil;
import com.oil.petro.eshop.utils.ProductHelper;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class RegisterController {

    @Autowired
    CategoryService categoryService;

    @Autowired
    ManufacturerService manufacturerService;

    @Autowired
    ProductService productService;

    @Autowired
    UserAccountService accountService;

    PaginationUtil paginationUtil = new PaginationUtil();
    ProductHelper productHelper = new ProductHelper();
    NavigationBar bar = new NavigationBar();
    NavigationMenu menu = new NavigationMenu();

    @RequestMapping(value = "/register", method = RequestMethod.GET)
    public ModelAndView registerUser(HttpServletRequest request) {

        NavigationBar bar = new NavigationBar();
        NavigationMenu menu = new NavigationMenu();

        Map<String, Object> model = new HashMap<String, Object>();
        UserBean userBean = new UserBean();
        bar.getNavigationBarDataModel(model, userBean, accountService);
        menu.getNavigationMenuDataModel(model, categoryService, manufacturerService);

        return new ModelAndView("account/registerPage", model);

    }

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public ModelAndView registerUserSubmit(@ModelAttribute("commandBean") UserBean userBean) {

        NavigationBar bar = new NavigationBar();
        NavigationMenu menu = new NavigationMenu();
        accountService.saveUser(userBean);

        Map<String, Object> model = new HashMap<String, Object>();

        bar.getNavigationBarDataModel(model, userBean, accountService);
        menu.getNavigationMenuDataModel(model, categoryService, manufacturerService);

        return new ModelAndView("account/registerCompletedPage", model);

    }

    @RequestMapping(value = "/editAccount", method = RequestMethod.GET)
    public ModelAndView editAccount(@ModelAttribute("commandBean") UserBean userBean, HttpServletRequest request) {
        Map<String, Object> model = new HashMap<>();

        userBean = accountService.getUserDetails(request.getUserPrincipal().getName());
        userBean.setUser(request.getUserPrincipal().getName());
        bar.getNavigationBarDataModel(model, userBean, accountService);
        menu.getNavigationMenuDataModel(model, categoryService, manufacturerService);

        return new ModelAndView("account/editAccount", model);
    }

    @RequestMapping(value = "/editAccount", method = RequestMethod.POST)
    public String onSubmitEditAccount(@ModelAttribute("commandBean") UserBean userBean, HttpServletRequest request) {
            
        Account account = accountService.getAccountByLogin(request.getUserPrincipal().getName());
        Person person = accountService.getPerson(account.getPersonId());
        
        account.setEmail(userBean.getEmail());
        if (userBean.getPassword().equals(userBean.getRepassword())) {
            account.setPassword(userBean.getPassword());
        }
        
        person.setAddress(userBean.getAddress());
        person.setCity(userBean.getCity());
        person.setPostCode(userBean.getPostCode());
        person.setFirstName(userBean.getFirstName());
        person.setLastName(userBean.getLastName());
        
        accountService.updatePerson(person);
        accountService.updateAccount(account);
        
        return "redirect:account.htm";// new ModelAndView("account/accountDetails", model);

    }

    @RequestMapping("/account")
    public ModelAndView viewUser(@ModelAttribute("commandBean") UserBean userBean, HttpServletRequest request) {

        Map<String, Object> model = new HashMap<>();
        userBean = accountService.getUserDetails(request.getUserPrincipal().getName());
        userBean.setUser(request.getUserPrincipal().getName());
        bar.getNavigationBarDataModel(model, userBean, accountService);
        menu.getNavigationMenuDataModel(model, categoryService, manufacturerService);

        return new ModelAndView("account/accountDetails", model);

    }

}
